package com.goldenautumn.prefabricate.activity.cart

import android.content.Context
import android.view.ViewGroup
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.DataBindingHolder
import com.goldenautumn.prefabricate.R
import com.goldenautumn.prefabricate.business.entity.CarGoodsInfoVO
import com.goldenautumn.prefabricate.databinding.ItemCartGoodsBinding
import com.goldenautumn.prefabricate.utils.loadImgUrl
import qncx.external.common.compat.isVisible

class CarGoodsAdapter  : BaseQuickAdapter<CarGoodsInfoVO, DataBindingHolder<ItemCartGoodsBinding>>() {

    var mOnNumberChangeListener:OnNumberChangeListener? = null

    override fun onBindViewHolder(holder: DataBindingHolder<ItemCartGoodsBinding>, position: Int, item: CarGoodsInfoVO?) {
        if(item == null) return
        var mBinding  = holder.binding
        mBinding.selectBtn.setImageResource(if(item.isCheck) R.mipmap.ic_car_select else R.mipmap.ic_car_not_select)
        mBinding.selectBtn.setOnClickListener {
            item.isCheck = !item.isCheck
            mOnNumberChangeListener?.onChangeNumber("${item.goods_id}")
            mBinding.selectBtn.setImageResource(if(item.isCheck) R.mipmap.ic_car_select else R.mipmap.ic_car_not_select)
        }
        mBinding.goodsSub.setOnClickListener {
            var number = mBinding.count.text.toString().toInt()
            if(number != 1) {
                var count = number - 1
                var sub = "$count"
                mBinding.count.text = sub
                item.total_num = count
                mOnNumberChangeListener?.onChangeNumber("${item.goods_id}","minus",item)
            }
        }

        mBinding.goodsAdd.setOnClickListener {
            var number = mBinding.count.text.toString().toInt()
            var count = number + 1
            var add = "$count"
            mBinding.count.text = add
            item.total_num = count
            mOnNumberChangeListener?.onChangeNumber("${item.goods_id}","add",item)
        }

//        mBinding.cover.loadImgUrl(item.goods_image,24)
        mBinding.cover.loadImgUrl("https://ts1.cn.mm.bing.net/th/id/R-C.b0089c802efa0179ae6f1a365473e2ed?rik=lfm%2bJ%2fspw5LX0w&riu=http%3a%2f%2fseopic.699pic.com%2fphoto%2f50079%2f2036.jpg_wh1200.jpg&ehk=qNQXICUl52BVrI2OOpTnCrI4wsTOPGByIeXp5MWc8nw%3d&risl=&pid=ImgRaw&r=0",24)
        mBinding.title.text = "${item.goods_name}"
        mBinding.spec.text = "${item.goods_sku?.goods_attr}"
        mBinding.spec.isVisible(item.goods_sku?.goods_attr?.length != 0)
        mBinding.price.text = (String.format(context.getString(R.string.currency), "${item?.goods_sku?.goods_price}"))
        mBinding.count.text = "${item.total_num}"
    }

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): DataBindingHolder<ItemCartGoodsBinding> {
        return DataBindingHolder(R.layout.item_cart_goods,parent)
    }

    interface OnNumberChangeListener {

        fun onChangeNumber(id:String,type:String? = null,item:CarGoodsInfoVO?? = null)

    }

}